import org.springframework.http.ResponseEntity;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;
import jakarta.servlet.http.HttpServletResponse;

@RestController
@RequestMapping("/api")
public class ReportController {
    private final ReportService reportService;

    public ReportController(ReportService reportService) {
        this.reportService = reportService;
    }

    @PostMapping("/students/import")
    public ResponseEntity<String> importStudents(@RequestParam("file") MultipartFile file) {
        try {
            int count = reportService.importStudents(file);
            return ResponseEntity.ok("成功导入" + count + "名学生");
        } catch (Exception e) {
            return ResponseEntity.badRequest().body("导入失败: " + e.getMessage());
        }
    }

    @GetMapping("/reports/analyze")
    public ResponseEntity<?> analyzeReports() {
        return ResponseEntity.ok(reportService.analyzeReports());
    }

    @GetMapping("/reports/export")
    public void export(HttpServletResponse response) throws IOException {
        reportService.exportToExcel(response);
    }
}
